import React, { Component } from 'react'

import './index.css'

import PropTypes from 'prop-types';

import { v4 as uuidAsV4 } from 'uuid'

export default class Header extends Component {
    
    static propsTypes = {
        addTodo: PropTypes.func.isRequired
    }
    
    handleKeyUp = event => {

        const {keyCode, target} = event;

        const value = target.value;

        if(keyCode !== 13) return;
        // 什么都没输入 返回
        if(value.trim()=== '') return;

        // 定义新的数据信息
        const data = {id: uuidAsV4(),name: value,done: false}

        this.props.addTodo(data);

        // 清空 input 的 值
        target.value = '';
    }

    render() {
        return (
            <div className="todo-header">

            <input onKeyUp = {this.handleKeyUp} type="text" placeholder="请输入你的任务名称，按回车键确认"/>

            </div>
        )
    }
}
